﻿Imports TransportadoraToledoDAL
Public Class LotacaoBLL
    Public Sub New()

    End Sub
    Dim objDAL As LotacaoDAL

#Region "Propriedades"


    Private lot_codigo As String
    Public Property pLot_codigo() As String
        Get
            Return lot_codigo
        End Get
        Set(ByVal value As String)
            lot_codigo = value
        End Set
    End Property

    Private vei_codigo As String
    Public Property pVei_codigo() As String
        Get
            Return vei_codigo
        End Get
        Set(ByVal value As String)
            vei_codigo = value
        End Set
    End Property

    Private lot_data As String
    Public Property pLot_data() As String
        Get
            Return lot_data
        End Get
        Set(ByVal value As String)
            lot_data = value
        End Set
    End Property

    Private lot_partida As String
    Public Property pLot_partida() As String
        Get
            Return lot_partida
        End Get
        Set(ByVal value As String)
            lot_partida = value
        End Set
    End Property


    Private lot_status As String
    Public Property pLot_status() As String
        Get
            Return lot_status
        End Get
        Set(ByVal value As String)
            lot_status = value
        End Set
    End Property



#End Region

#Region "Métodos Cargas"
    Public Function carregaListaCargas(ByVal lot_codigo As Integer) As DataTable
        Dim objDAL As New LotacaoDAL
        Dim tab As DataTable
        Try
            tab = objDAL.localizarCargas(lot_codigo)
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function carregarVeiculo() As DataTable
        Dim objDAL As New LotacaoDAL
        Dim tab As New DataTable()
        Try
            tab = objDAL.carregarveiculo(vei_codigo)
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Sub excluirCarga(ByVal car_codigo As Integer, ByVal lot_codigo As Integer)
        Try
            Dim objDAL = New LotacaoDAL
            If (car_codigo <> 0) Then
                objDAL.excluirCarga(car_codigo, lot_codigo)
            Else
                Throw New Exception("Selecione uma carga")
            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Public Function localizarCargasDisponiveis(ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Try
            Dim objDAL = New LotacaoDAL()
            tab = objDAL.localizarCargasDisponiveis(descricao)
        Catch ex As Exception
            Throw ex
        End Try
        Return tab
    End Function

    Public Function localizarDadosCarga(ByVal car_codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            Dim objDAL = New LotacaoDAL()
            tab = objDAL.localizarDadosCarga(car_codigo)
        Catch ex As Exception
            Throw ex
        End Try
        Return tab
    End Function

    Public Sub inserirCargas(ByVal lot_codigo, ByVal car_codigo, ByVal age_codigo)
        Try
            Dim objDAL = New LotacaoDAL
            If (car_codigo <> 0) Then
                objDAL.inserirCargas(lot_codigo, car_codigo, age_codigo)
            Else
                Throw New Exception("Não há cargas a serem inseridas")
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub alterarCargas(ByVal lot_codigo, ByVal car_codigo, ByVal age_codigo)
        Try
            Dim objDAL = New LotacaoDAL
            If (car_codigo <> 0) Then
                objDAL.alterarCargas(lot_codigo, car_codigo, age_codigo)
            Else
                Throw New Exception("Não há cargas a serem inseridas")
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

#Region "Métodos"
    Public Sub inserir()
        Try
            If (vei_codigo = 0) Then
                Throw New Exception("Informe o veículo")
            ElseIf (Not IsDate(lot_data)) Then
                Throw New Exception("Informe o data da lotação.")
            ElseIf (Not IsDate(lot_partida)) Then
                Throw New Exception("Informe o data da partida.")
            ElseIf (Date.Parse(lot_data) > Date.Parse(lot_partida)) Then
                Throw New Exception("A Data de lotação não pode ser maior que a data prevista de partida.")
            Else
                objDAL = New LotacaoDAL
                Me.lot_codigo = objDAL.inserirLotacao(Me.vei_codigo, Me.lot_data, Me.lot_partida, Me.lot_status)
                carregarVeiculo()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Public Sub alterar()
        Try
            If (Not IsDate(lot_data)) Then
                Throw New Exception("Informe a data da lotação.")
            ElseIf (Not IsDate(lot_partida)) Then
                Throw New Exception("Informe a data de partida.")
            ElseIf (lot_codigo = 0) Then
                Throw New Exception("Não existe lotação para alterar.")
            Else
                objDAL = New LotacaoDAL
                objDAL.alterarLotacao(Me.lot_codigo, Me.lot_data, Me.lot_partida, Me.lot_status)

            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Public Sub excluir()
        Try
            If (lot_codigo = 0) Then
                Throw New Exception("Não existe lotação para excluir")
            Else
                objDAL = New LotacaoDAL
                objDAL.excluirLotacao(Me.lot_codigo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try
            objDAL = New LotacaoDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("lot_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function
    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New LotacaoDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("lot_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("lot_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New LotacaoDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("lot_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("lot_codigo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Public Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New LotacaoDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.lot_codigo = codigo
                Me.lot_data = tab.Rows(0)("lot_data").ToString()
                Me.lot_partida = tab.Rows(0)("lot_partida").ToString()
                Me.vei_codigo = tab.Rows(0)("vei_codigo").ToString()
                Me.lot_status = tab.Rows(0)("lot_status").ToString()

            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub


    Public Function localizar(ByVal placa As String, ByVal inicioCar As Date, ByVal fimCar As Date, ByVal inicioPrev As Date, ByVal fimPrev As Date, ByVal status As String) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New LotacaoDAL
            tab = objDAL.localizar(placa, inicioCar, fimCar, inicioPrev, fimPrev, status)
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Function

    'Function consultaveiculosdisponiveis() As DataTable
    '    Dim objDAL As New LotacaoDAL
    '    Dim tab As New DataTable
    '    Try
    '        tab = objDAL.consultaveiculosdisponiveis()
    '    Catch ex As Exception

    '    End Try
    '    Return tab
    'End Function

#End Region
End Class
